package rensoft.android.presenter;

import java.util.ArrayList;

import static rensoft.android.lang.AndroidLang.event;
import static rensoft.java.lang.Lang.list;
import static rensoft.java.lang.Lang.map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ViewAnimator;
import rensoft.android.MainActivityPresenter;
import rensoft.java.collections.List;
import rensoft.java.collections.Map;
import rensoft.java.event.Event;

public abstract class ApplicationPresenterBase extends MainActivityPresenter implements
    IApplicationPresenter {

  private final Map<String, ApplicationView> viewPresenters = map();
  private static final String HISTORY_STATE_KEY = "history_state_key";
  private ApplicationView currentViewPresenter;
  private final Event<ApplicationView> onViewChange = event();
  private final List<String> viewHistory = list();

  public ApplicationPresenterBase(Activity activity, int layoutId, int requestFailedMessageId) {
    super(activity, layoutId, requestFailedMessageId);
  }

  public void addApplicationView(String id, ApplicationView applicationView) {
    viewPresenters.put(id, applicationView);
  }

  public Event<ApplicationView> getOnViewChange() {
    return onViewChange;
  }

  public void goBack() {
    setBackAnimation();
    viewHistory.removeLast();
    displayView(viewPresenters.value(viewHistory.last()));
  }

  @Override
  public void onBackPressed() {
    if (viewHistory.size() > 0)
      goBack();
    else super.onBackPressed();
  }

  @SuppressWarnings("unchecked")
  @Override
  public void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    state.putStringArrayList(HISTORY_STATE_KEY, (ArrayList<String>) viewHistory);
  }

  protected void displayNextView(ApplicationView view) {
    viewHistory.add(view.getId());
    setNextAnimation();
    displayView(view);
  }

  protected abstract ViewAnimator getAnimator();

  protected ApplicationView getCurrentViewPresenter() {
    return currentViewPresenter;
  }

  protected abstract ApplicationView getMainPresenter();

  @Override
  protected void onCreateFirstTime() {
    super.onCreateFirstTime();
    viewHistory.add(getMainPresenter().getId());
    setCurrentView(getMainPresenter(), true);
  }

  @Override
  protected void onCreateRestore(Bundle state) {
    super.onCreateRestore(state);
    if (state.containsKey(HISTORY_STATE_KEY))
      viewHistory.addAll(state.getStringArrayList(HISTORY_STATE_KEY));
    setCurrentView(viewPresenters.value(viewHistory.last()), true);
  }

  protected abstract void setBackAnimation();

  protected abstract void setNextAnimation();

  private void displayView(ApplicationView view) {
    currentViewPresenter.onApplicationViewGoesToBackground();
    setCurrentView(view, false);
    getAnimator().setDisplayedChild(1);
    getAnimator().removeViewAt(0);
  }

  private void setCurrentView(ApplicationView view, boolean isCreate) {
    currentViewPresenter = view;
    getAnimator().addView(currentViewPresenter.createView());
    currentViewPresenter.onApplicationViewGoesToForeground(isCreate);
    fire(onViewChange, currentViewPresenter);
  }
}
